<template>
  <div>
    <el-form :model="ruleForm" ref="ruleForm" class="ruleForm">
      <table
        border="1"
        cellpadding
        style="width: 100%"
        cellpadding="0"
        cellspacing="0"
      >
        <thead>
        <tr style="height: 44px">
          <th :colspan="2" width="150">项目</th>
          <th width="150">限度</th>
          <th width="150" v-for="(item, index) in indexList"
              :key="index">
            {{ item }}
          </th>
        </tr>
        </thead>
        <tbody>
        <tr v-for="(val, ind) in ruleForm.table_info" :key="ind" style="height: 44px">
          <td v-if="val.boo" align="center" :colspan="val.col" :rowspan="val.row" :width="val.boo ? '50' : '200'">
            {{ val.parent_name }}
            <el-button
              v-if="val.col === 2 && val.limit_commit === 1"
              type="text"
              class="el-icon-plus"
              @click="handlAdd(ind)"
            ></el-button>
          </td>
          <td v-if="val.col === 1" :colspan="val.col" width="150">
            <el-form-item :prop="'table_info.' + ind + '.children_name'"
                          :rules="[ { required: true, message: '请输入内容', trigger: 'blur' }]">
              <div style="display: flex">
                <el-input v-model="val.children_name">
                </el-input>
                <el-button
                  style="width: 20px"
                  type="text"
                  icon="el-icon-plus"
                  @click="handAddCol(ind,val.parent_id)"
                ></el-button>
                <el-button
                  style="width: 20px"
                  type="text"
                  icon="el-icon-minus"
                  @click="handDeleCol(ind,val.parent_id,val.row)"
                ></el-button>
              </div>

            </el-form-item>
          </td>
          <td align="center" width="150">
            <el-form-item :prop="'table_info.' + ind + '.limit_value'"
                          :rules="[ { required: true, message: '请输入内容', trigger: 'blur' }]">
              <el-input v-if="val.limit_commit === 1" v-model="val.limit_value"/>
              <span v-else>{{val.limit_value}}</span>
            </el-form-item>
          </td>
          <template v-for="(tab, tind) in indexList">
            <td align="center" :key="tind" width="150">
              <el-form-item v-if="val.point_info && val.point_info[tab]"
                            :prop="'table_info.' + ind + '.point_info'"
                            :rules="[ { required: true, message: '请输入内容', trigger: 'blur' }]">
                <el-input v-if="val.point_info[tab].if_commit === 1" v-model="val.point_info[tab].value"/>
                <span v-else>{{val.point_info[tab].value}}</span>
              </el-form-item>
            </td>
          </template>
        </tr>
        </tbody>
      </table>
    </el-form>
  </div>
</template>

<script>
  export default {
    name: 'tableList',
    props: {
      ruleForm: {
        type: Object,
        default() {
          return {
            point_table: [],
            table_info: []
          }
        }
      },
      point: {
        type: Object,
        default() {
          return {}
        }
      }
    },
    data() {
      return {
        indexList: [],
        value: '222'
      }
    },
    //是计算属性，也可以理解为一个方法。
    computed: {},

    //属性是手动定义的所需监听的值
    watch: {},

    //生命周期 - 创建完成（访问当前this实例）
    created() {
    },

    //生命周期 - 挂载完成（访问DOM元素）
    mounted() {
      let a = this.point.sample_point_num
      let b = this.point.sample_point_unit
      let c
      if (b === 1) {
        c = a + '天'
      } else {
        c = a + '月'
      }
      if (this.ruleForm.point_table) {
        let index = this.ruleForm.point_table.findIndex(p => p === c)
        if (index !== -1) {
          for (let i = 0; i < this.ruleForm.point_table.length; i++) {
            if (i <= index) {
              this.indexList.push(this.ruleForm.point_table[i])
            }
          }
        }
      }
      for (let j = 0; j < this.ruleForm.table_info.length; j++) {
        let aa = this.ruleForm.table_info[j]
        let bb = { if_commit: 1, value: '' }
        for (let i = 0; i < this.indexList.length; i++) {
          let key = this.indexList[i]
          if (aa.point_info[key] === undefined) {
            this.$set(aa.point_info, key, bb)
          }
        }
      }

    },

    //是Vue实例对象上绑定的方法，供当前Vue组件作用域内使用，
    methods: {

      handlAdd(index) {
        this.ruleForm.table_info[index].col = 1
      },
      handAddCol(index, id) {
        let bbb = JSON.parse(JSON.stringify(this.ruleForm.table_info[index]))
        let aaa = {}
        let idx = index + 1
        for (let i = 0; i < this.indexList.length; i++) {
          let key = this.indexList[i]
          aaa[key] = {
            if_commit: 1,
            value: ''
          }
        }
        let data = {
          boo: false,
          children_id: '',
          children_name: '',
          limit_commit: 1,
          limit_value: '',
          parent_id: bbb.parent_id,
          parent_name: bbb.parent_name,
          point_info: aaa
        }
        this.ruleForm.table_info[index].row = this.ruleForm.table_info[index].row + 1
        this.ruleForm.table_info.splice(idx, 0, data)
        this.ruleForm.table_info[idx].row = this.ruleForm.table_info[index].row
        this.ruleForm.table_info.filter(a => a.parent_id === id).forEach(item => {
          item.row = this.ruleForm.table_info[idx].row
        })
        this.ruleForm.table_info[idx].col = 1
      },

      handDeleCol(index, id, row) {
        let aaa = this.ruleForm.table_info.filter(a => a.parent_id === this.ruleForm.table_info[index].parent_id)
        aaa = JSON.parse(JSON.stringify(aaa))
        if (aaa.length === 1) {
          let a = JSON.parse(JSON.stringify(this.ruleForm.table_info[index]))
          a.row = 1
          a.col = 2
          this.ruleForm.table_info.splice(index, 1)
          this.ruleForm.table_info.splice(index, 0, a)
        } else {
          this.ruleForm.table_info.splice(index, 1)
          let arr = []
          for (let i = 0; i < this.ruleForm.table_info.length; i++) {
            let obj = this.ruleForm.table_info[i]
            if (obj.parent_id === id) {
              arr.push(i)
              obj.row = row - 1
            }
          }
          let as = arr[0]
          if (this.ruleForm.table_info[as].boo === false) {
            this.$set(this.ruleForm.table_info[as], 'boo', true)
          }
        }

      },

      submitFormsss() {
        let boolean = false
        this.$refs.ruleForm.validate((valid) => {
          if (valid) {
            boolean = true
          } else {
            boolean = false
          }
        })
        if (boolean) {
          return 1
        }
      }

    }
  }

</script>

<style scoped>
  .ruleForm ::v-deep .el-form-item {
    margin-bottom: 0 !important;
  }

  ::v-deep .ruleForm .el-input-group__append {
    padding: 0 !important;
  }
</style>
